/**
 * 
 */
package com.alisoft.nano.seda.impl;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

import com.alisoft.nano.seda.Event;
import com.alisoft.nano.seda.EventHandler;

/**
 * @author zhimin.chenzm, anping.yinap
 *
 */
public class EventHandlerWrapper<T> implements EventHandler<T> {
    private final EventHandler<T> eventHandler;

    public EventHandlerWrapper(EventHandler<T> eventHandler) {
        super();
        this.eventHandler = eventHandler;
    }
    
    public void process(Event<T> event) {
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        if (!threadMXBean.isThreadContentionMonitoringEnabled()) {
            threadMXBean.setThreadContentionMonitoringEnabled(true);    
        }
        
        long currentThreadId = Thread.currentThread().getId();
                
        event.setStartTime(System.nanoTime());
        event.setStartUserCPUTime(threadMXBean.getThreadUserTime(currentThreadId));
        eventHandler.process(event);
        event.setEndUserCPUTime(threadMXBean.getThreadUserTime(currentThreadId));
        event.setEndTime(System.nanoTime());
        
    }
}
